home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.3a1 / Bricks ƒ / Bricks.p < prev    next >
Encoding:
Text File  |  1994-11-09  |  6.3 KB  |  242 lines  |  [TEXT/PJMM]

  1. {Bricks}
  2. {}
  3. {Demo program for SAT 2.3, by Ingemar Ragnemalm november 1994}
  4. {}
  5. {The purpose of this demo is to test and demonstrate the support for what I call "resting" sprites.}
  6. {If a sprite neither moves, changes face, overlaps a changing sprite nor changes its place in the}
  7. {sprite list, then SAT can avoid drawing it.}
  8. {}
  9. {For programs where all sprites change in every frame (which is the case in many arcade games)}
  10. {then this is not of any interest at all, and will just slow things down. For other programs, for}
  11. {example board games, Tetris-style games etc, this technique is very useful.}
  12. {}
  13. {For the moment (SAT 2.3d3), you get the new system by calling SATRun2 instead of SATRun. The name}
  14. {of SATRun2 is likely to change in the future.}
  15. {}
  16. {When you start Bricks, the sprites will not be initialied in order, so since VPositionSort}
  17. {is active, they will sort during the first frames. This slows the program down for the first}
  18. {few seconds. If this is a problem, it can be avoided by either turning off sorting or}
  19. {creating the sprite in the proper places from the start.}
  20. {}
  21. {Bug note: When using "fast graphics", the cursor will cause some "mouse droppings". This is best avoided}
  22. {by hiding the cursor and repllacing it with a SAT sprite. The "proper" way to avoid the problem is to}
  23. {call ShieldCursor, but that will make the cursor flicker a lot.}
  24.  
  25. program Bricks;
  26.     uses
  27. {$ifc UNDEFINED THINK_PASCAL}
  28.         Types, QuickDraw, Events, Windows, Dialogs,
  29.         Fonts, DiskInit,
  30.         TextEdit, Traps, Desk, Memory, SegLoad, Scrap,
  31.         ToolUtils, OSEvents, OSUtils, Menus, Resources,
  32.         StandardFile, GestaltEqu, Files, Errors,
  33. {$endc}
  34.         SAT;
  35.  
  36.     const
  37.         kNumBricks = 30;
  38.     var
  39.         brickFace: array[0..kNumBricks] of FacePtr;
  40.         myEvent: EventRecord;
  41.         whichWindow: WindowPtr;
  42.         appleMenu, fileMenu: MenuHandle;
  43.         gUseStagger, gDone, gUseFast, gAllowBackground: Boolean;
  44.         i: integer;
  45.         sp, found: SpritePtr;
  46.         theSelection: Longint;
  47.         where: Point;
  48.         theKey: Char;
  49.         whichPart: Integer;
  50.         hasEvent: Boolean;
  51.  
  52.     procedure InitBricks;
  53.         var
  54.             i: integer;
  55.     begin
  56.         for i := 0 to kNumBricks - 1 do
  57.             brickFace[i] := SATGetFace(i + 128);
  58.     end;
  59.  
  60.     procedure HandleRestingBrick (me: SpritePtr);
  61.     begin
  62.     end;
  63.  
  64.     procedure HandleFollowMouse (me: SpritePtr);
  65.         var
  66.             mousePos: Point;
  67.     begin
  68.         if Button then
  69.             begin
  70.                 GetMouse(mousePos);
  71.                 me^.position.h := me^.position.h + mousePos.h - me^.speed.h;
  72.                 me^.position.v := me^.position.v + mousePos.v - me^.speed.v;
  73.                 me^.speed := mousePos;
  74.             end
  75.         else
  76.             me^.task := @HandleRestingBrick;
  77.     end;
  78.  
  79.     procedure SetupBrick (me: SpritePtr);
  80.     begin
  81.         me^.task := @HandleRestingBrick;
  82.         me^.face := brickFace[me^.kind];
  83.     end;
  84.  
  85.     procedure SynchMenus;
  86.     begin
  87.         CheckItem(fileMenu, 1, not gUseStagger);
  88.         CheckItem(fileMenu, 2, gUseStagger);
  89.         CheckItem(fileMenu, 3, gUseFast);
  90.         CheckItem(fileMenu, 4, gAllowBackground);
  91.     end;
  92.  
  93.     procedure MenuSelection (theSelection: Longint);
  94.         var
  95.             name: Str255;
  96.             saveport: GrafPtr;
  97.     begin
  98.         case HiWord(theSelection) of
  99.             128: 
  100.                 begin
  101.                     if LoWord(theSelection) = 1 then
  102.                         ReportStr('Experimental SAT demo program. Resting sprites allow good speed with many sprites!')
  103.                     else
  104.                         begin
  105.                             GetPort(saveport);
  106.                             GetItem(appleMenu, 1, name);    (* get name *)
  107.                             if OpenDeskAcc(name) = 0 then (* run the desk accessory *)
  108.                                 ;
  109.                             SetPort(saveport);
  110.                         end;
  111.                 end;
  112.             129: 
  113.                 case LoWord(theSelection) of
  114.                     1: 
  115.                         gUseStagger := false;
  116.                     2: 
  117.                         gUseStagger := true;
  118.                     3: 
  119.                         gUseFast := not gUseFast;
  120.                     4: 
  121.                         gAllowBackground := not gAllowBackground;
  122.                     6: 
  123.                         gDone := true;
  124.                 end; {case MenuSelect}
  125.             otherwise
  126.         end;
  127.         SynchMenus;
  128.         HiLiteMenu(0);
  129.     end;
  130.  
  131. begin
  132. {CodeWarrior needs inits here}
  133. {$IFC UNDEFINED THINK_PASCAL}
  134.         InitGraf(@qd.thePort);
  135.         InitFonts;
  136.         InitWindows;
  137.         InitMenus;
  138.         TEInit;
  139.         InitDialogs(nil);
  140.         MaxApplZone;
  141. {$ENDC}
  142.  
  143.     InitBricks;
  144.  
  145.     SATInit(128, 129, 512, 342);
  146.     SATSetPortScreen;
  147.     gUseStagger := true;
  148.     gAllowBackground := true;
  149.     gUseFast := false;
  150.  
  151.     for i := 0 to kNumBricks - 1 do
  152.         sp := SATNewSprite(i, Rand(gSAT.offSizeH - 48), Rand(gSAT.offSizeV - 64), @SetupBrick);
  153.     for i := 0 to kNumBricks - 1 do
  154.         sp := SATNewSprite(i, Rand(gSAT.offSizeH - 48), Rand(gSAT.offSizeV - 64), @SetupBrick);
  155.  
  156.     appleMenu := NewMenu(128, stringof(char($14)));
  157.     AppendMenu(appleMenu, 'About Bricks demo…;(-');
  158.     AddResMenu(appleMenu, 'DRVR');
  159.     InsertMenu(appleMenu, 0);            { put apple menu at end of menu bar }
  160.  
  161.     fileMenu := NewMenu(129, 'File');
  162.     AppendMenu(fileMenu, 'RunSAT/1;RunSAT2/2;Fast graphics/F;Allow background tasks;(-;Quit/Q');
  163.     InsertMenu(fileMenu, 0);            { put file menu at end of menu bar }
  164.     DrawMenuBar;
  165.     SynchMenus;
  166.  
  167.     InitCursor;
  168.  
  169.     repeat
  170. {if gUseFast then}
  171. {ShieldCursor(gSAT.bounds, Point(0));}
  172.         if gUseStagger then
  173.             SATRun2(gUseFast)
  174.         else
  175.             SATRun(gUseFast);
  176. {if gUseFast then}
  177. {ShowCursor;}
  178.  
  179.         if gAllowBackground then
  180.             begin
  181.                 SystemTask;
  182.                 hasEvent := GetNextEvent(everyEvent, myEvent)
  183.             end
  184.         else
  185.             hasEvent := GetOSEvent(everyEvent, myEvent);
  186.         if hasEvent then
  187.                                     {mDownMask + updateMask + keyDownMask}
  188.             case myEvent.what of
  189.                 updateEvt: 
  190.                     if WindowPtr(myEvent.message) = gSAT.wind then
  191.                         begin
  192.                             BeginUpdate(gSAT.wind);
  193.                             SATRedraw;
  194.                             EndUpdate(gSAT.wind);
  195.                         end;
  196.                 keyDown: 
  197.                     begin
  198.                         theKey := char(BitAnd(myEvent.message, charCodeMask));
  199.                         if (BitAnd(myEvent.modifiers, cmdKey) <> 0) then
  200.                             MenuSelection(MenuKey(theKey))
  201.                         else
  202. {DoKey(theKey, theEvent.modifiers)}
  203.                             ;
  204.                     end;
  205.                 mouseDown: 
  206.                     begin
  207.                         whichPart := FindWindow(myEvent.where, whichWindow);
  208.                         case whichPart of
  209.                             inMenuBar: 
  210.                                 begin
  211.                                     theSelection := MenuSelect(myEvent.where);
  212.                                     MenuSelection(theSelection);
  213.                                 end;
  214.                             inSysWindow: 
  215.                                 SystemClick(myEvent, whichWindow);
  216.                             inContent: 
  217.                                 begin
  218.                                     found := nil;
  219.                                     sp := gSAT.sRoot;
  220.                                     GetMouse(where);
  221.                                     while sp <> nil do
  222.                                         begin
  223.                                             if PtInRect(where, sp^.r) then
  224.                                                 found := sp;
  225.                                             sp := sp^.next;
  226.                                         end;
  227.                                     if found <> nil then
  228.                                         begin
  229.                                             found^.task := @HandleFollowMouse;
  230.                                             found^.speed := where; {not speed, but storage for old mouse position}
  231.                                         end;
  232.                                 end;
  233.                             otherwise
  234.                         end; {case whichPart}
  235.                     end;
  236.  
  237.                 otherwise
  238.             end;{case myEvent.what}
  239.     until gDone;
  240.  
  241.     SATSoundShutup;
  242. end.